package edu.whu.xterm.activity;

import edu.whu.xterm.Article;
import edu.whu.xterm.R;
import edu.whu.xterm.http.RequestBuilder;
import greendroid.app.GDActivity;

import java.io.UnsupportedEncodingException;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class PostArticleActivity extends GDActivity {

	static final int DIALOG_POST_ARTICLE_PROGRESS = 1;

	Article preArticle;

	PostType type;

	EditText title;
	EditText content;
	CheckBox anonymous;
	EditText address;
	Button post;

	public enum PostType {
		REPLY_ARTICLE, //
		NEW_ARTICLE, //
		NEW_MAIL, //
		REPLY_MAIL, ;

		public static final String POST_TYPE = "post_type";

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.activity_post_artical);
		preArticle = getIntent().getParcelableExtra(TopicAndReplyWforumActivity.BUNDLE_KEY_DATA);
		type = (PostType) getIntent().getSerializableExtra(PostType.POST_TYPE);

		title = (EditText) findViewById(R.id.post_title);
		content = (EditText) findViewById(R.id.post_content);
		anonymous = (CheckBox) findViewById(R.id.post_anonymous);
		address = (EditText) findViewById(R.id.post_address);
		post = (Button) findViewById(R.id.post_button);

		post.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Editable ctnt = content.getText();
				boolean hasContent = ctnt != null && ctnt.toString().trim().length() > 0;
				if (hasContent) {
					new PostAsyncTask().execute();
				} else {
					Toast.makeText(getApplicationContext(), R.string.write_something,
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		setUpView();
	}

	String generateReContent(Article preArticle, CharSequence txt) {
		StringBuilder sb = new StringBuilder();
		sb.append(txt);
		sb.append("\r\n");
		sb.append("【在");
		sb.append(preArticle.author);
		sb.append("大作中提到】\r\n");
		sb.append(": ");
		sb.append(((String) preArticle.content).replace("\n", "\r\n: "));
		sb.append("\r\n");
		sb.append(getString(R.string.from_hterm));
		return sb.toString();
	}

	private void setUpView() {
		if (preArticle != null) {
			String t = preArticle.title;
			if (!t.startsWith("Re")) {
				t = "Re: " + t;
			}
			title.setHint(t);
		}

	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		switch (id) {
		case DIALOG_POST_ARTICLE_PROGRESS: {
			return ProgressDialog.show(this, null, getString(R.string.progress_txt_sending));
		}
		default:
			break;
		}
		return super.onCreateDialog(id, args);
	}

	private class PostAsyncTask extends AsyncTask<Void, Void, Boolean> {

		private static final String BOARD = "board";
		private static final String RE_ID = "reID";
		private static final String FONT = "font";
		private static final String SUBJECT = "subject";
		private static final String CONTENT = "content";
		private static final String SIGNATURE = "signature";

		@Override
		protected void onPreExecute() {
			showDialog(DIALOG_POST_ARTICLE_PROGRESS);
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			String subject = "";
			if (TextUtils.isEmpty(title.getText())) {
				subject = title.getHint().toString();
			} else {
				subject = title.getText().toString();
			}
			try {
				RequestBuilder
						.getBuilder()
						.replyRequest(preArticle, subject,
								generateReContent(preArticle, content.getText())).call();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return Boolean.FALSE;
			} catch (Exception e) {
				e.printStackTrace();
				return Boolean.FALSE;
			}

			return Boolean.TRUE;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result.booleanValue()) {
				dismissDialog(DIALOG_POST_ARTICLE_PROGRESS);
				finish();
			} else {
				Toast.makeText(getApplicationContext(), R.string.progress_txt_send_failed,
						Toast.LENGTH_SHORT).show();
			}
		}
	}
}
